/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazEditarAsignatura.java
 *
 * Created on 18/10/2011, 01:27:28 PM
 */
package presentacion;

import Paneles.PanelGeneral;
import business.AreaBO;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import business.AsignaturaBO;
import dominio.Asignatura;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

/**
 *
 * @author SEBASTIAN
 */
public class InterfazEditarAsignatura extends javax.swing.JFrame {

    private ImageIcon icono;
    AreaBO areaBo = new AreaBO();
    AsignaturaBO asignaturaBo = new AsignaturaBO();
    int ok;

    /** Creates new form InterfazEditarAsignatura */
    public InterfazEditarAsignatura() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        cargarListaGrados();
        cargarListaAreas();
        cargarListaIntensidadHoraria();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        comboGrados = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        comboAreas = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        comboAsignaturas = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        entradaNombreAsignatura = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        comboGradoModificado = new javax.swing.JComboBox();
        comboAreaModificada = new javax.swing.JComboBox();
        comboIntensidadHorariaModificada = new javax.swing.JComboBox();
        botonAceptar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setTitle("Edición de Asignaturas");
        setResizable(false);

        jLabel2.setText("Buscar Asignatura:");

        jLabel3.setText("Grado al que se dicta:");

        comboGrados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboGrados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboGradosActionPerformed(evt);
            }
        });

        jLabel4.setText("Area:");

        comboAreas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboAreas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboAreasActionPerformed(evt);
            }
        });

        jLabel5.setText("Nombre de la Asignatura:");

        comboAsignaturas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboAsignaturas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboAsignaturasActionPerformed(evt);
            }
        });

        jLabel7.setText("Nuevo nombre de la asignatura:");

        jLabel8.setText("Grado al que se dicta:");

        jLabel9.setText("Area:");

        jLabel10.setText("Intensidad Horaria (en horas a la semana)");

        comboGradoModificado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        comboAreaModificada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        comboIntensidadHorariaModificada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        botonAceptar.setText("Modificar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        jButton3.setText("Eliminar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(192, 192, 192)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(161, 161, 161)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboGrados, 0, 484, Short.MAX_VALUE)
                            .addComponent(comboAreas, 0, 484, Short.MAX_VALUE)
                            .addComponent(comboAsignaturas, 0, 484, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(comboIntensidadHorariaModificada, javax.swing.GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE)
                            .addComponent(comboAreaModificada, javax.swing.GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE)
                            .addComponent(comboGradoModificado, javax.swing.GroupLayout.Alignment.LEADING, 0, 555, Short.MAX_VALUE)
                            .addComponent(entradaNombreAsignatura, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(botonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 348, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(357, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(233, 233, 233))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboGrados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboAreas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboAsignaturas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(entradaNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(comboGradoModificado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(comboAreaModificada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(comboIntensidadHorariaModificada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(botonAceptar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed

        ok = 0;
        Asignatura tst;

        if (entradaNombreAsignatura.getText().equals("") || comboIntensidadHorariaModificada.getSelectedIndex() == 0 || comboGradoModificado.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Debe digitar un nombre, seleccionar una Intensidad Horaria, escoger un grado y un area", null, JOptionPane.ERROR_MESSAGE);
        }
        else {
            tst = asignaturaBo.consultarAsignatura(entradaNombreAsignatura.getText(), comboGradoModificado.getSelectedIndex() - 1);
            if (tst == null || (tst.getNombreAsignatura().equals(entradaNombreAsignatura.getText()) && tst.getIdGrado().equals(comboGradoModificado.getSelectedIndex() - 1))) {
                ok = asignaturaBo.actualizarAsignatura((comboAsignaturas.getSelectedItem() + ""), comboGrados.getSelectedIndex() - 1, entradaNombreAsignatura.getText(), comboIntensidadHorariaModificada.getSelectedIndex(), comboGradoModificado.getSelectedIndex() - 1, (comboAreaModificada.getSelectedItem() + ""));
                entradaNombreAsignatura.setText("");
                cargarListaGrados();
                cargarListaAreas();
                cargarListaIntensidadHoraria();
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La modificación de la asignatura fue exitosa", "Modificación exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar la asignatura (Asignaturas Repetidas)", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar la asignatura", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                }
            }

        }







    }//GEN-LAST:event_botonAceptarActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if (comboAsignaturas.getSelectedIndex() == -1 || comboGrados.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "No hay asignaturas que borrar", "Eliminación fallida", JOptionPane.ERROR_MESSAGE);
        }
        else {
            ok = asignaturaBo.encontrarAsignatura(comboAsignaturas.getSelectedItem() + "", comboGrados.getSelectedIndex() - 1);
            if (ok != 0) {
                asignaturaBo.borrarAsignatura(ok);
                JOptionPane.showMessageDialog(this, "Eliminado correctamente", "Eliminación correcta", JOptionPane.OK_OPTION);
                cargarListaGrados();
                cargarListaAreas();
                cargarListaIntensidadHoraria();
            }
        }


    }//GEN-LAST:event_jButton3ActionPerformed

    private void comboAreasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboAreasActionPerformed
        cargarListaAsignaturas();
    }//GEN-LAST:event_comboAreasActionPerformed

    private void comboAsignaturasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboAsignaturasActionPerformed
        if (comboAsignaturas.getSelectedItem() != null) {
            entradaNombreAsignatura.setText(comboAsignaturas.getSelectedItem() + "");
        }
        else {
            entradaNombreAsignatura.setText("");
        }
        comboGradoModificado.setSelectedIndex(comboGrados.getSelectedIndex());
        comboAreaModificada.setSelectedIndex(comboAreas.getSelectedIndex());
    }//GEN-LAST:event_comboAsignaturasActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void comboGradosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboGradosActionPerformed
        cargarListaAsignaturas();
    }//GEN-LAST:event_comboGradosActionPerformed

    private void cargarListaGrados() {
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboGrados.getModel();
        DefaultComboBoxModel modelo1 = (DefaultComboBoxModel) comboGradoModificado.getModel();
        modelo.removeAllElements();
        modelo1.removeAllElements();
        modelo.addElement("Seleccione");
        modelo.addElement("0.  Transicion");
        modelo.addElement("1.  Primero");
        modelo.addElement("2.  Segundo");
        modelo.addElement("3.  Tercero");
        modelo.addElement("4.  Cuarto");
        modelo.addElement("5.  Quinto");
        modelo.addElement("6.  Sexto");
        modelo.addElement("7.  Séptimo");
        modelo.addElement("8.  Octavo");
        modelo.addElement("9.  Noveno");
        modelo.addElement("10.  Decimo");
        modelo.addElement("11.  Once");

        modelo1.addElement("Seleccione");
        modelo1.addElement("0.  Transicion");
        modelo1.addElement("1.  Primero");
        modelo1.addElement("2.  Segundo");
        modelo1.addElement("3.  Tercero");
        modelo1.addElement("4.  Cuarto");
        modelo1.addElement("5.  Quinto");
        modelo1.addElement("6.  Sexto");
        modelo1.addElement("7.  Séptimo");
        modelo1.addElement("8.  Octavo");
        modelo1.addElement("9.  Noveno");
        modelo1.addElement("10.  Decimo");
        modelo1.addElement("11.  Once");

    }

    private void cargarListaAreas() {
        List l = areaBo.consultarAreas();
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboAreas.getModel();
        DefaultComboBoxModel modelo1 = (DefaultComboBoxModel) comboAreaModificada.getModel();
        modelo.removeAllElements();
        modelo1.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));
            modelo1.addElement(l.get(i));

        }
        comboAreas.setModel(modelo);
    }

    private void cargarListaAsignaturas() {
        List a = asignaturaBo.consultarAsignaturasAreaGrado(comboAreas.getSelectedItem() + "", comboGrados.getSelectedIndex() - 1);

        DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboAsignaturas.getModel();
        modelo.removeAllElements();
        for (int i = 0; i < a.size(); i++) {
            modelo.addElement(a.get(i));

        }
        comboAsignaturas.setModel(modelo);


    }

    private void cargarListaIntensidadHoraria() {
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboIntensidadHorariaModificada.getModel();
        modelo.removeAllElements();
        modelo.addElement("Seleccione");
        modelo.addElement("1 Hora");
        modelo.addElement("2 Horas");
        modelo.addElement("3 Horas");
        modelo.addElement("4 Horas");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazEditarAsignatura().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JComboBox comboAreaModificada;
    private javax.swing.JComboBox comboAreas;
    private javax.swing.JComboBox comboAsignaturas;
    private javax.swing.JComboBox comboGradoModificado;
    private javax.swing.JComboBox comboGrados;
    private javax.swing.JComboBox comboIntensidadHorariaModificada;
    private javax.swing.JTextField entradaNombreAsignatura;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables
}
